package org.xlx.xlx_oa.reimburse.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.reimburse.convertor.ReimburseConvertor;
import org.xlx.xlx_oa.reimburse.domain.ReimburseImage;
import org.xlx.xlx_oa.reimburse.domain.dto.ReimburseImageDto;
import org.xlx.xlx_oa.reimburse.mapper.ReimburseImageMapper;

import java.util.List;

@Service
public class ReimburseImageService {
    private ReimburseConvertor reimburseConvertor = ReimburseConvertor.INSTANCE;
    @Autowired
    private ReimburseImageMapper imageMapper;

    /**
     * 上传报销单图片
     */
    public Long uploadImage(String fileUrl) {
        ReimburseImage image = new ReimburseImage();
        image.setFileUrl(fileUrl);
        imageMapper.insert(image);
        return image.getId();
    }

    /**
     * 报销单图片与报销单匹配
     */
    public void matchImage(Long applyId,List<Long> imageIds){
        UpdateWrapper<ReimburseImage> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(ReimburseImage::getReimburseId, applyId);
        imageMapper.delete(wrapper);
        UpdateWrapper<ReimburseImage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().in(ReimburseImage::getId, imageIds);
        ReimburseImage image = new ReimburseImage();
        image.setReimburseId(applyId);
        imageMapper.update(image,updateWrapper);
    }

    /**
     * 根据报销单获得图片
     */
    public List<ReimburseImageDto> getImagesByApplyId(Long applyId){
        QueryWrapper<ReimburseImage> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ReimburseImage::getReimburseId,applyId);
        List<ReimburseImage> images = imageMapper.selectList(wrapper);
        List<ReimburseImageDto> dtos = reimburseConvertor.convertToImageDtoList(images);
        return dtos;
    }
}
